package ${location}.common;

import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * 通用返回类
 *
 * @author ${author}
 */
public class ResponseResult<T> implements Serializable {
	/**
	 * 相应状态码
	 */
	private int code;
	/**
	 * 提示信息
	 */
	private String msg;
	/**
	 * 携带数据
	 */
	private T data;

	/**
	 * 成功 携带数据
	 *
	 * @param <T>
	 * @return
	 */
	public static <T> ResponseResult<T> success() {
		return new ResponseResult<T>(HttpCode._200_OK);
	}

	/**
	 * 请求成功携带数据
	 *
	 * @param data
	 * @param <T>
	 * @return
	 */
	public static <T> ResponseResult<T> success(T data) {
		return new ResponseResult<>(HttpCode._200_OK, data);
	}


	/**
	 * 自定义响应码, 信息并返回数据,适合特殊的情况
	 *
	 * @param httpCode
	 * @param data
	 * @param message
	 * @param <T>
	 * @return
	 */
	public static <T> ResponseResult<T> customize(HttpCode httpCode, T data, String message) {
		return new ResponseResult<T>(httpCode.getCode(),
				StringUtils.isBlank(message) ? httpCode.getMsg() : message, data);
	}

	/**
	 * 默认错误
	 *
	 * @param <T>
	 * @return
	 */
	public static <T> ResponseResult<T> error() {
		return new ResponseResult<T>(HttpCode._503_SERVICE_UNAVAILABLE);
	}


	/**
	 * 自定义错误消息
	 *
	 * @param message
	 * @return
	 */
	public static <T> ResponseResult<T> error(String message) {
		return new ResponseResult<T>(HttpCode._400_BAD_REQUEST.getCode(),
				StringUtils.isBlank(message) ? HttpCode._400_BAD_REQUEST.getMsg() : message, null);
	}

	/**
	 * 自定义错误码
	 *
	 * @param httpCode
	 * @return
	 */
	public static <T> ResponseResult<T> error(HttpCode httpCode) {
		return new ResponseResult<T>(httpCode);
	}

	/**
	 * 自定义消息和状态码
	 *
	 * @param httpCode
	 * @param message
	 * @return
	 */
	public static <T> ResponseResult<T> error(HttpCode httpCode, String message) {
		return new ResponseResult<T>(httpCode.getCode(),
				StringUtils.isBlank(message) ? httpCode.getMsg() : message, null);
	}

	public ResponseResult() {
	}

	public ResponseResult(int code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public ResponseResult(HttpCode httpCode, T data) {
		this.code = httpCode.getCode();
		this.msg = httpCode.getMsg();
		this.data = data;
	}

	public ResponseResult(HttpCode httpCode) {
		this.code = httpCode.getCode();
		this.msg = httpCode.getMsg();
	}


	public int getCode() {
		return code;
	}

	public String getMsg() {
		return msg;
	}

	public T getData() {
		return data;
	}

	@Override
	public String toString() {
		return "ResponseResult{" +
				"code=" + code +
				", msg='" + msg + '\'' +
				", data=" + data +
				'}';
	}

	private static final long serialVersionUID = 1L;
}
